$(function(){
	provStart("add");
	// 选择省份时发生事件
	$("#addprovince").bind("change",function(){
		cityStart("add");
	});
	// 选择市级时发生事件
	$("#addcity").bind("change",function(){
		distStart("add");
	});
	
	provStart("edit");
	$("#editprovince").bind("change",function(){
		cityStart("edit");
	});
	$("#editcity").bind("change",function(){
		distStart("edit");
	});
	
	$('.close').click(function(){
		$('.regsuc_cjcd_box').hide(10);
		$('.login_main').hide(10);
	});
	/*单位名称_begin*/
	$('#companyName').focus(function(){
		if($("#companyName").val() == '请输入单位名称'){
			$("#companyName").val("").css("color","#4e4e4e");
		}else{
			$("#companyName").css("color","#4e4e4e").attr('value',$(this).val());
		}		
	});
	$('#companyName').focusout(function(){
		if($("#companyName").val() == ''){
			$(this).attr('value','请输入单位名称').css("color","#c4c4c4");
			$(this).val('请输入单位名称');
		}
	});
	/*单位名称_end*/	

	/*单位简介_begin*/
	$('#remark').focus(function(){
		if($("#remark").val() == '请输入单位简介，500字以内...'){
			$("#remark").val("").css("color","#4e4e4e");
		}else{
			$("#remark").css("color","#4e4e4e").attr('value',$(this).val());
		}		
	});
	$('#remark').focusout(function(){
		if($("#remark").val() == ''){
			$(this).attr('value','请输入单位简介，500字以内...').css("color","#c4c4c4");
			$(this).val('请输入单位简介，500字以内...');
		}
	});
	/*单位简介_end*/	
});
function editCompany(){
	/*$("#editprovince").val(pid);
	setTimeout(function(){
		cityStart("edit");
	},100);
	setTimeout(function(){
		distStart("edit");
	},100);*/
	$("#editCompany").show();
	$("#createCompany").hide();
}
function createCompany(){
	$("#createCompany").show();
	$("#editCompany").hide();
}

function save(type){
	var companyName = $('#companyName').val();
	var province = $('#addprovince').val();
	var country = $('#addcountry').val();
	var city = $('#addcity').val();
	var remark = $('#remark').val();
	if(remark == '请输入单位简介，500字以内...')
		remark = '';
	if(companyName == '请输入单位名称'){
		alert("请输入单位名称");
	}else if(6>companyName.length){
		alert("单位名称不能小于6个字");
	} else if(companyName.length>20){
		alert("单位名称不能大于20个字");
	}else if(remark.length>500){
		alert("单位简介不能大于500个字");
	}else{
		$.ajax({
			url : contextPath+ 'user/company/add',
			data : {
				companyName : companyName,
				province:province,
				country:country,
				city:city,
				country:country,
				remark:remark,
				pId:companyId,
				type:type
			},
			type : 'POST',
			success : function(data) {
				if(data.opResult == '0'){
					alert("创建单位成功");
					$('.regsuc_cjcd_box').hide();
					if(type==1){
						location.href=contextPath+"user/m/userflow.jsp";
					}else{
						try{
							goToPage(1);
						}catch(e){
							location.reload();
						}
					}
				}else {
					alert(data.msgDesc);
				}
			}
		});
	}
}

function edit(type){
	var companyName = $('#editcompanyName').val();
	var province = $('#editprovince').val();
	var country = $('#editcountry').val();
	var city = $('#editcity').val();
	var remark = $('#editremark').val();
	if(remark == '请输入单位简介，500字以内...')
		remark = '';
	if(companyName == '请输入单位名称'){
		alert("请输入单位名称");
	}else if(6>companyName.length){
		alert("单位名称不能小于6个字");
	} else if(companyName.length>20){
		alert("单位名称不能大于20个字");
	}else if(remark.length>500){
		alert("单位简介不能大于500个字");
	}else{
		$.ajax({
			url : contextPath+ 'user/company/edit',
			data : {
				companyName : companyName,
				province:province,
				country:country,
				city:city,
				country:country,
				remark:remark,
				companyId:companyId
			},
			type : 'POST',
			success : function(data) {
				if(data.opResult == '0'){
					alert("编辑单位成功");
					location.reload();
				}else {
					alert(data.msgDesc);
				}
			}
		});
	}
}

function exit(type){
	if (confirm("是否退出？")){
		document.exitform.submit();
		//location.href=contextPath+'user/m/switch.jsp';
	}
}
function setStatus(status){
	$.ajax({
		url : contextPath+ 'user/company/updatestatus',
		data : {
			status : status
		},
		type : 'POST',
		success : function(data) {
			alert(data.msgDesc);
			location.href=contextPath+'user/m/currentcompany.jsp'
		}
	});
}


var provStart=function(type){
	$.ajax({
		url : contextPath+ '/user/geo/list',
		data : {
			parent : -1
		},
		type : 'POST',
		success : function(list) {
			$("#"+type+"city").hide();
			$("#"+type+"country").hide();
			if(list!=null && list.length>0){
				$("#"+type+"province").empty().show();
				var option="";
				for(var i=0;i<list.length;i++){
					var province = list[i];
					if(type=="edit" && pid == province.gbCode ){
						option +="<option selected='selected' value='"+province.gbCode+"'>"+province.gbName+"</option>";
					}else{
						option +="<option value='"+province.gbCode+"'>"+province.gbName+"</option>";
					}
				}
				$("#"+type+"province").append(option);
			}
			setTimeout(function(){
				cityStart(type);
			},100);
		}
	});
};
//赋值市级函数
var cityStart=function(type){
	var prov_id=$("#"+type+"province").val();
	$.ajax({
		url : contextPath+ '/user/geo/list',
		data : {
			parent : prov_id
		},
		type : 'POST',
		success : function(list) {
			$("#"+type+"city").hide();
			$("#"+type+"country").hide();
			if(list!=null && list.length>0){
				$("#"+type+"city").empty().show();
				var option="";
				for(var i=0;i<list.length;i++){
					var city = list[i];
					if(type=="edit" && cid == city.gbCode ){
						option +="<option selected='selected' value='"+city.gbCode+"'>"+city.gbName+"</option>";
					}else{
						option +="<option value='"+city.gbCode+"'>"+city.gbName+"</option>";
					}
				}
				$("#"+type+"city").append(option);
			}
			setTimeout(function(){
				distStart(type);
			},100);
		}
	});
};
//赋值地区（县）函数
var distStart=function(type){
	var city_id=$("#"+type+"city").val();
	$.ajax({
		url : contextPath+ '/user/geo/list',
		data : {
			parent : city_id
		},
		type : 'POST',
		success : function(list) {
			$("#"+type+"country").hide();
			if(list!=null && list.length>0){
				$("#"+type+"country").empty().show();
				var option="";
				for(var i=0;i<list.length;i++){
					var country = list[i];
					if(type=="edit" && aid == country.gbCode){
						option +="<option selected='selected' value='"+country.gbCode+"'>"+country.gbName+"</option>";
					}else{
						option +="<option value='"+country.gbCode+"'>"+country.gbName+"</option>";
					}
				}
				$("#"+type+"country").append(option);
			}
		}
	});
};